package com.osmino.contacts.entity;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import com.osmino.contacts.R;

public final class Group {

	public static final String PREF_KEY_ICON = "group_icon_";
	public static final String PREF_KEY_RINGTONE = "group_ringtone_";
	public static final String ID_COLUMN = ContactsContract.Groups._ID;
	public static final String TITLE_COLUMN = ContactsContract.Groups.TITLE;
	public static final String ACCOUNT_TYPE_COLUMN = ContactsContract.Groups.ACCOUNT_TYPE;
	public static final String ACCOUNT_NAME_COLUMN = ContactsContract.Groups.ACCOUNT_NAME;
	public static final long NO_GROUP = -1;
	public static final long DRAFT = -2;
	public static final int NO_ICON = -1;
	public static final int ICON_DEFAULT = R.drawable.group_ic_68;
	public final long id;
	public String title;
	public LocalAccount account;
	private int mIconResource;
	private Uri mRingtone;
	private final List<Contact> mContacts;

	private Group(long id, String title, String accountType, String accountName, int iconRes, Uri ringtone,
			List<Contact> contacts) {
		this.id = id;
		this.title = title;
		account = new LocalAccount(accountType, accountName);
		mIconResource = iconRes;
		mRingtone = ringtone;
		mContacts = contacts;
	}

	public Group(Context context, Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(ID_COLUMN);
		id = cursor.getLong(columnIndex);
		columnIndex = cursor.getColumnIndex(TITLE_COLUMN);
		title = cursor.getString(columnIndex);
		columnIndex = cursor.getColumnIndex(ACCOUNT_TYPE_COLUMN);
		String accountType = cursor.getString(columnIndex);
		columnIndex = cursor.getColumnIndex(ACCOUNT_NAME_COLUMN);
		String accountName = cursor.getString(columnIndex);
		account = new LocalAccount(accountType, accountName);
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String iconKey = getPreferenceKeyForIcon(id);
		mIconResource = preferences.getInt(iconKey, NO_ICON);
		String ringtoneKey = getPreferenceKeyForRingtone(id);
		String unparsedRingtone = preferences.getString(ringtoneKey, null);
		mRingtone = null;
		if (unparsedRingtone != null) {
			mRingtone = Uri.parse(unparsedRingtone);
		}
		mContacts = new ArrayList<Contact>();
	}

	public void addContact(Contact contact) {
		mContacts.add(contact);
	}

	public void removeContact(Contact contact) {
		mContacts.remove(contact);
	}

	public List<Contact> getContacts() {
		return mContacts;
	}

	public int sizeOfContactList() {
		return mContacts.size();
	}

	public int getIconResource() {
		return mIconResource;
	}

	public void setIconResource(Context context, int iconResource) {
		mIconResource = iconResource;
		if (id >= 0) {
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			String key = getPreferenceKeyForIcon(id);
			preferences.edit().putInt(key, mIconResource).commit();
		}
	}

	public static void setIconResource(long id, Context context, int iconResource) {
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String key = getPreferenceKeyForIcon(id);
		preferences.edit().putInt(key, iconResource).commit();
	}

	public Uri getRingtone() {
		return mRingtone;
	}

	public void setRingtone(Context context, Uri ringtone) {
		mRingtone = ringtone;
		if (id >= 0) {
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			String key = getPreferenceKeyForRingtone(id);
			String uriToString = null;
			if (mRingtone != null) {
				uriToString = mRingtone.toString();
			}
			preferences.edit().putString(key, uriToString).commit();
		}
	}

	public static void setRingtone(long id, Context context, Uri ringtone) {
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		String key = getPreferenceKeyForRingtone(id);
		String uriToString = null;
		if (ringtone != null) {
			uriToString = ringtone.toString();
		}
		preferences.edit().putString(key, uriToString).commit();
	}

	public static String getPreferenceKeyForIcon(long id) {
		return PREF_KEY_ICON + id;
	}

	public static String getPreferenceKeyForRingtone(long id) {
		return PREF_KEY_RINGTONE + id;
	}

	public static Group createUngroupedGroup(Context context) {
		long id = NO_GROUP;
		String title = context.getString(R.string.ungrouped);
		String accountName = null;
		String accountType = null;
		int iconRes = -1;
		Uri ringtone = null;
		List<Contact> contacts = new ArrayList<Contact>();
		return new Group(id, title, accountType, accountName, iconRes, ringtone, contacts);
	}

	public static Group createDraftGroup(Context context) {
		long id = DRAFT;
		String title = context.getString(R.string.ungrouped);
		String accountName = null;
		String accountType = null;
		int iconRes = -1;
		Uri ringtone = null;
		List<Contact> contacts = new ArrayList<Contact>();
		return new Group(id, title, accountType, accountName, iconRes, ringtone, contacts);
	}
}
